class Solution:
    def getWinner(self, arr: list, k: int) -> int:
        def nextIndex(arr, i):
            return (i + 1) % len(arr)

        if k >= len(arr):
            return max(arr)
        head = 0
        tail = len(arr) - 1
        win = 0
        champion = -1
        while win < k:
            i = head
            j = nextIndex(arr, head)
            winner = max(arr[i], arr[j])
            loser = min(arr[i], arr[j])
            head = nextIndex(arr, head)
            tail = nextIndex(arr, tail)
            arr[head] = winner
            arr[tail] = loser
            if champion != winner:
                win = 0
                champion = winner
            win += 1

        return champion
